package com.ydd.moudle.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ydd.R;
import com.ydd.base.BaseFragment;
import com.ydd.model.event.LoginEvent;
import com.ydd.model.event.QuitLoginEvent;
import com.ydd.model.user.User;
import com.ydd.moudle.me.LoginActivity;
import com.ydd.moudle.me.SettingActivity;
import com.ydd.moudle.me.VideoSettingActivity;
import com.ydd.network.mina.SessionManager;
import com.ydd.share.ShareDialog;
import com.ydd.util.db.SPManager;
import com.ydd.util.glide.GlideManager;
import com.ydd.util.user.UserManager;
import com.ydd.widegt.MyQrCodeDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cn.sharesdk.framework.Platform;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by tao on 17-3-11.
 * 个人中心
 */

public class MeFragment extends BaseFragment implements View.OnClickListener {
    /**
     * UI
     */
    private View mContentView;
    private RelativeLayout mLoginLayout;
    private CircleImageView mPhotoView;
    private CircleImageView mUserPhotoView;
    private TextView mLoginInfoView;
    private TextView mLoginView;
    private RelativeLayout mLoginedLayout;
    private TextView mUserNameView;
    private TextView mTickView;
    private TextView mVideoPlayerView;
    private TextView mShareView;
    private TextView mQrCodeView;
    private TextView mSettingView;

    private boolean isLogin;

    public MeFragment() {

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             Bundle savedInstanceState) {
        mContext = getActivity();
        mContentView = inflater.inflate(R.layout.fragment_me_layout, container, false);
        EventBus.getDefault().register(this);
        initHeaderView(getString(R.string.me), mContentView);

        initView();
        showLoginedInfo();
        return mContentView;
    }

    private void initView() {

        mLoginLayout = (RelativeLayout) mContentView.findViewById(R.id.login_layout);
        mLoginedLayout = (RelativeLayout) mContentView.findViewById(R.id.logined_layout);
        mPhotoView = (CircleImageView) mContentView.findViewById(R.id.photo_view);
        mLoginView = (TextView) mContentView.findViewById(R.id.login_view);
        mVideoPlayerView = (TextView) mContentView.findViewById(R.id.video_setting_view);
        mShareView = (TextView) mContentView.findViewById(R.id.share_friend_view);
        mQrCodeView = (TextView) mContentView.findViewById(R.id.my_qrcode_view);
        mLoginInfoView = (TextView) mContentView.findViewById(R.id.login_info_view);
        mUserNameView = (TextView) mContentView.findViewById(R.id.username_view);
        mUserPhotoView = (CircleImageView) mContentView.findViewById(R.id.user_photo_view);
        mTickView = (TextView) mContentView.findViewById(R.id.tick_view);
        mSettingView = (TextView) mContentView.findViewById(R.id.setting_view);

        mLoginLayout.setOnClickListener(this);
        mLoginedLayout.setOnClickListener(this);
        mPhotoView.setOnClickListener(this);
        mLoginView.setOnClickListener(this);
        mVideoPlayerView.setOnClickListener(this);
        mShareView.setOnClickListener(this);
        mQrCodeView.setOnClickListener(this);
        mSettingView.setOnClickListener(this);
        mUserPhotoView.setOnClickListener(this);

    }

    private void showLoginedInfo() {

        String userName = SPManager.getInstance().getString(SPManager.USER_NAME, "");
        String userPhotoUrl = SPManager.getInstance().getString(SPManager.USER_PHOTO_URL, "");
        boolean autoLogin = SPManager.getInstance().getBoolean(SPManager.AUTOMATIC__LOGIN_USER, false);
        if (autoLogin) {
            mLoginLayout.setVisibility(View.GONE);
            mLoginedLayout.setVisibility(View.VISIBLE);
            mUserNameView.setText(userName);
            if (!TextUtils.isEmpty(userPhotoUrl)) {
                GlideManager.loadImageView(mContext, userPhotoUrl, mUserPhotoView);
            }
            SPManager.getInstance().putBoolean(SPManager.IS_LOGIN_USER, true);
            isLogin = SPManager.getInstance().getBoolean(SPManager.IS_LOGIN_USER, false);
        } else {
            mLoginLayout.setVisibility(View.VISIBLE);
            mLoginedLayout.setVisibility(View.GONE);
            SPManager.getInstance().putBoolean(SPManager.IS_LOGIN_USER, false);
            isLogin = SPManager.getInstance().getBoolean(SPManager.IS_LOGIN_USER, false);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.video_setting_view:
                mContext.startActivity(new Intent(mContext, VideoSettingActivity.class));
                break;
            case R.id.login_view:
                //未登陆，则跳轉到登陸页面
                if (!isLogin) {
                    toLogin();
                }
                break;

            case R.id.my_qrcode_view:
                if (!isLogin) {
                    toLogin();
                } else {
                    //已登陆根据用户ID生成二维码显示
                    MyQrCodeDialog dialog = new MyQrCodeDialog(mContext);
                    dialog.show();

                }
                break;
            case R.id.share_friend_view:
                showShareDialog();
                break;
            case R.id.setting_view:
                SessionManager.getInstance().writeToService("123");
                Intent intent = new Intent(getActivity(), SettingActivity.class);
                startActivity(intent);
                break;
            case R.id.user_photo_view:
                upLoadPhoto();
                break;
            default:
                break;
        }
    }

    private void upLoadPhoto() {

    }

    private void showShareDialog() {
        ShareDialog dialog = new ShareDialog(mContext, false);
        dialog.setShareType(Platform.SHARE_IMAGE);
        dialog.setShareTitle("小说网");
        dialog.setShareTitleUrl("http://www.23us.com/");
        dialog.setShareText("小说网");
        dialog.setShareSite("story");
        dialog.setShareSiteUrl("http://www.23us.com/");
        dialog.setImagePhoto("http://www.23us.com/themes/xiaoshuo/logo.gif");
        dialog.show();
    }

    private void toLogin() {
        Intent intent = new Intent(mContext, LoginActivity.class);
        mContext.startActivity(intent);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLoginEvent(LoginEvent event) {
        showStatus();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onQuitLoginEvent(QuitLoginEvent event) {
        showStatus();
    }

    private void showStatus() {
        User user = UserManager.getInstance().getUser();
        if (user != null) {
            isLogin = true;
            String userName = user.getData().getName();
            mLoginLayout.setVisibility(View.GONE);
            mLoginedLayout.setVisibility(View.VISIBLE);
            mUserNameView.setText(userName);
            if (!TextUtils.isEmpty(user.getData().getPhotoUrl())) {
                GlideManager.loadImageView(mContext, user.getData().getPhotoUrl(), mUserPhotoView);
            }
        } else {
            isLogin = false;
            mLoginLayout.setVisibility(View.VISIBLE);
            mLoginedLayout.setVisibility(View.GONE);
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(mContext);
    }
}
